package ex55.mm.flow.impound.quote.common.kit;

import org.apache.commons.lang3.StringUtils;

public class StringFormat {

    private final String format;
    private final Object[] args;

    private StringFormat(String format, Object[] args) {
        this.format = format;
        this.args = args;
    }

    public static String string(String format, Object... args) {
        String[] split = StringUtils.split(format, "\\{\\}", 0);
        StringBuilder sb = new StringBuilder();
        if (split.length > 1) {
            for (int i = 0; i < split.length; i++) {
                String arg = "{}";
                if (i + 1 <= args.length) {
                    arg = CastKit.stringOf(args[i]);
                }
                sb.append(split[i]).append(arg);
            }
        } else if (split.length == 1) {
            sb.append(split[0]);
        }
        if (split.length < args.length) {
            sb.append(" ~ {");
            for (int i = split.length; i < args.length; i++) {
                sb.append(" [ ").append(CastKit.stringOf(args[i])).append(" ],");
            }
            sb.append(" } ");
        }
        return sb.toString();
    }

    public String format() {
        return string(format, args);
    }


    public static StringFormat create(String format, Object... args) {
        return new StringFormat(format, args);
    }
}
